<template>
  <div v-loading="loading">
    <el-upload v-model:file-list="fileList" :show-file-list="false" :headers="header" :limit="limit" :on-exceed="exceedTrips" multiple action="/api/image/upload" :before-upload="beforeUpload" :on-success="handleSuccess" :on-remove="handleRemove" list-type="picture">
      <el-button type="primary">点击上传</el-button>
      <template #tip>
        <div class="el-upload__tip">可上传jpg/png文件，文件大小需小于10Ms,视频可传MP4和AVI,不大于100M</div>
      </template>
    </el-upload>
    <el-card class="file-box" v-for="item in modelList" :key="item.model.Pk">
      <div class="file-list">
        <el-image fit="cover" style="display: block; width: 100%; height: 100%" :src="item.file.url" />
      </div>
      <div class="file-discription">
        <el-descriptions :border="true" :column="1">
          <el-descriptions-item label="图片名称">{{ item.model.ImageName }}</el-descriptions-item>
          <el-descriptions-item label="类型">{{ item.model.FileType }}</el-descriptions-item>
          <el-descriptions-item label="大小">{{ (item.model.Size! / 1024 / 1024).toFixed(2) }}MB</el-descriptions-item>
          <el-descriptions-item label="创建人">{{ item.model.Creator!._v2 }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">{{ item.model.CreateDate }}</el-descriptions-item>
          <el-descriptions-item label="系列">
            <el-select style="width: 200px" filterable v-model="item.model.Series" placeholder="选择系列">
              <el-option default-first-option v-for="seriesItem in allSeries" :key="seriesItem.Pk" :label="seriesItem.SeriesName" :value="seriesItem.Pk" />
            </el-select>
          </el-descriptions-item>
          <el-descriptions-item label="所需积分"> <el-input-number style="width: 200px" v-model="item.model.Price" :min="0" :max="1000"></el-input-number> </el-descriptions-item>
          <el-descriptions-item label="说明"> <el-input style="width: 200px" v-model="item.model.Note" /></el-descriptions-item>
          <el-descriptions-item label="操作">
            <el-button @click="confirm(item.model, false)">修改</el-button>
            <el-button type="primary" @click="confirm(item.model, true)">修改并发布</el-button>
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-card>
  </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';

import { ElMessage, type UploadFile, type UploadFiles, type UploadProps, type UploadUserFile } from 'element-plus';
import { useUserStore } from '@/stores/userInfo';
import type { ImageModel } from '@/model/imagemodel';
import { save } from '@/api/image';
import { listSeries } from '@/api/series';

import type { SeriesModel } from '@/model/seriesmodel';
const fileList = ref<UploadUserFile[]>([]);
const allSeries = ref<SeriesModel[]>([]);
const store = useUserStore();
const loading = ref(false);
const modelList = ref<{ model: ImageModel; file: UploadFile }[]>([]);
const limit = ref(30);
const header = ref<any>({
  Authorization: store.token,
});
listSeries().then((r) => {
  allSeries.value = r.Data!.filter((r) => !r.Disable);
});
const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles);
};
const handleSuccess = (response: any, uploadFile: UploadFile, uploadFiles: UploadFiles) => {
  uploadFiles[0].status;
  modelList.value.push({ model: response.Data[0], file: uploadFile });
  if (uploadFiles.filter((r) => r.status === 'uploading' || r.status === 'ready').length === 0) {
    loading.value = false;
  }
};
const exceedTrips = () => {
  ElMessage.error('单次最多上传' + limit.value + '个!');
};
const beforeUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type === 'image/jpeg' || rawFile.type === 'image/png') {
    if (rawFile.size / 1024 / 1024 > 10) {
      ElMessage.error('图片大小不能超过10MB!');
      return false;
    }
  } else if (rawFile.type.startsWith('video')) {
    if (rawFile.size / 1024 / 1024 > 100) {
      ElMessage.error('视频大小不能超过100MB!');
      return false;
    }
  } else {
    ElMessage.error('图片只支持JPG格式或PNG格式，视频只支持MP4');
    return false;
  }
  loading.value = true;
  return true;
};
const confirm = (item: ImageModel, is_publish: boolean) => {
  if (item.Price === 0) {
    ElMessage.warning('填入所需分数!');
    return;
  }
  if (!item.Series) {
    ElMessage.warning('选择系列');
    return;
  }
  if (is_publish) {
    item.Publish = is_publish;
  }
  loading.value = true;
  save(item).then((res) => {
    ElMessage.success('修改成功!');
    const index = fileList.value.findIndex((r) => ((r.response as any).Data[0] as ImageModel).Pk === item.Pk);
    const index2 = modelList.value.findIndex((r) => r.model.Pk === item.Pk);
    fileList.value.splice(index, 1);
    modelList.value.splice(index2, 1);
    loading.value = false;
  });
};
</script>
<style>
.file-box {
  height: 440px;
  margin-top: 10px;
}
.file-list {
  height: 400px;
  width: 600px;
  float: left;
}
.file-discription {
  margin-left: 10px;
  float: left;
}
</style>
